package net.towee.client.com.lobby.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.ExtFlowPanel;
import net.towee.client.widgets.ListItemWidget;
import net.towee.client.widgets.OpacityAnimation;
import net.towee.client.widgets.SpanPanel;
import net.towee.client.widgets.UnorderedListWidget;
import net.towee.model.account.Connection;
import net.towee.model.account.ConnectionType;
import net.towee.model.account.Gender;
import net.towee.model.account.impl.ConnectionImpl;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LobbyProfileWhoPage extends FlowPanel {
	
	private class ConnectionsManager extends FlowPanel implements ClickHandler {
		private final FlowPanel container;
		private final Button addButton;
		private final UnorderedListWidget types;
		private final OpacityAnimation animation;
		private final List<TextBox> connBoxes;
		
		public ConnectionsManager() {
			this.connBoxes = new ArrayList<TextBox>();
			this.container = new FlowPanel();
			this.addButton = new Button( constants.addContact() );
			this.types = new UnorderedListWidget();
			this.animation = new OpacityAnimation(types, false, 165);
			this.setStyleName(resource.css().lobbyProfileConnectionManager());
			
			FlowPanel addContainer = new FlowPanel();
			addContainer.add(addButton);
			addContainer.add(types);
			add(container);
			add(addContainer);
			
			container.getElement().getStyle().setProperty("marginTop", "7px");
			addContainer.getElement().getStyle().setProperty("border", "1px solid #eee");
			addContainer.setStyleName(resource.css().lobbyProfileConnectTypeSelector());
			
			addButton.setStyleName(resource.css().lobbyProfileAddConnectionButton());
			addButton.addClickHandler(this);
			
			// Create types selector
			types.addItem(constants.getString(ConnectionType.EMAIL+"")).setLayoutData(ConnectionType.EMAIL);
			types.addItem(constants.getString(ConnectionType.ADDRESS+"")).setLayoutData(ConnectionType.ADDRESS);
			types.addItem(constants.getString(ConnectionType.GTALK+"")).setLayoutData(ConnectionType.GTALK);
			types.addItem(constants.getString(ConnectionType.JABBER+"")).setLayoutData(ConnectionType.JABBER);
			types.addItem(constants.getString(ConnectionType.ICQ+"")).setLayoutData(ConnectionType.ICQ);
			types.addItem(constants.getString(ConnectionType.MOBILE+"")).setLayoutData(ConnectionType.MOBILE);
			types.addItem(constants.getString(ConnectionType.PHONE+"")).setLayoutData(ConnectionType.PHONE);
			types.addClickHandler(this);
		}

		@Override
		public void onClick(ClickEvent event) {
			Object source = event.getSource();
			animation.run(200);
			
			if(!source.equals(addButton)) {
				ConnectionType connection = (ConnectionType)(((ListItemWidget)source).getLayoutData());
				addInputContact(connection, ((ListItemWidget)source).getElement().getInnerText());
			}
		}
		
		public void addInputContact(ConnectionType type, String name){
			FlowPanel row = new FlowPanel();
			
			TextBox box = new TextBox();
			box.setLayoutData(type);
			connBoxes.add(box);
			
			row.add(new SpanPanel(name));
			row.add(new SpanPanel(" - "));
			row.add(box);
			row.setStyleName(resource.css().lobbyProfileConnectForm());
			container.add(row);
		}
		
		public List<Connection> getConnections(){
			List<Connection> connections = new ArrayList<Connection>();
			
			for(TextBox box : connBoxes) {
				if(!box.getText().equals(""))
					connections.add(new ConnectionImpl((ConnectionType) box.getLayoutData(), box.getText()));
			}
			
			return connections;
		}
	}
	
	private ToweeConstants constants;
	private ClientResource resource;
	
	private TextBox firstName = new TextBox();
	private TextBox lastName = new TextBox();
	private ListBox sex;
	private ListBox dobDay;
	private ListBox dobMonth;
	private ListBox dobYear;
	private ConnectionsManager connManager;

	public LobbyProfileWhoPage(ClientResource resource, ToweeConstants constants){
		this.resource = resource;
		this.constants = constants;
		
		add(createProfileField(constants.first_name(), firstName));
		add(createProfileField(constants.second_name(), lastName));
		add(createSexSelector());
		add(createDobSelector());
		add(createConnectionManager());
		
		this.firstName = (TextBox)((ComplexPanel)getWidget(0)).getWidget(1);
		this.lastName = (TextBox)((ComplexPanel)getWidget(1)).getWidget(1);
	}
	
	private Widget createConnectionManager() {
		FlowPanel field = new FlowPanel();
		this.connManager = new ConnectionsManager();
		
		field.setStyleName(resource.css().lobbyProfileField());
		field.add(new ExtFlowPanel(constants.contacts(), resource.css().lobbyProfileFieldName()));
		field.add(connManager);
		return field;
	}

	private Widget createDobSelector() {
		FlowPanel field = new FlowPanel();
		field.setStyleName(resource.css().lobbyProfileField());
		field.add(new ExtFlowPanel(constants.dob(), resource.css().lobbyProfileFieldName()));
		
		dobDay = new ListBox();
		for(int i=1;i<=31;i++)
			dobDay.addItem(i+"");
		field.add(dobDay);
		
		dobMonth = new ListBox();
		dobMonth.getElement().getStyle().setProperty("left", "335px");
		dobMonth.addItem(constants.january());
		dobMonth.addItem(constants.february());
		dobMonth.addItem(constants.march());
		dobMonth.addItem(constants.april());
		dobMonth.addItem(constants.may());
		dobMonth.addItem(constants.june());
		dobMonth.addItem(constants.jule());
		dobMonth.addItem(constants.august());
		dobMonth.addItem(constants.september());
		dobMonth.addItem(constants.octember());
		dobMonth.addItem(constants.november());
		dobMonth.addItem(constants.december());
		field.add(dobMonth);
		
		dobYear = new ListBox();
		dobYear.getElement().getStyle().setProperty("left", "470px");
		for(int i=2011;i>=1930;i--)
			dobYear.addItem(i+"");
		field.add(dobYear);
	    
		return field;
	}

	private Widget createSexSelector() {
		FlowPanel field = new FlowPanel();
		field.setStyleName(resource.css().lobbyProfileField());
		
		field.add(new ExtFlowPanel(constants.sex(), resource.css().lobbyProfileFieldName()));
		
		sex = new ListBox();
	    sex.addItem(constants.male());
	    sex.addItem(constants.female());
	    field.add(sex);
	    
		return field;
	}

	private FlowPanel createProfileField(String name, Widget w) {
		FlowPanel field = new FlowPanel();
		field.setStyleName(resource.css().lobbyProfileField());
		field.add(new ExtFlowPanel(name, resource.css().lobbyProfileFieldName()));
		field.add(w);
		
		return field;
	}
	
	public String getFirstName() {
		return firstName.getText();
	}

	public String getLastName() {
		return lastName.getText();
	}
	
	@SuppressWarnings("deprecation")
	public Date getDob() {
		Date dob = new Date();
		dob.setDate( dobDay.getSelectedIndex()+1);
		dob.setMonth( dobMonth.getSelectedIndex()+1);
		dob.setYear( Integer.parseInt( dobYear.getItemText( dobYear.getSelectedIndex() )));
		return dob;
	}
	
	public Gender getGender() {
		if(sex.getSelectedIndex() == 0)
			return Gender.MALE;
		else if(sex.getSelectedIndex() == 1)
			return Gender.FEMALE;
		else
			return Gender.UNKNOWN;
	}
	
	public List<Connection> getConnections() {
		return connManager.getConnections();
	}
	
	public void setFirstName(String name) {
		firstName.setText(name);
	}
	
	public void setLastName(String name) {
		lastName.setText(name);
	}
}
